package com.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dao.UserDAO;
import com.model.User;
import com.util.DB;

public class UserDAOImpl implements UserDAO {

	// 连接数据库
	// private static Connection conn = DB.getConnection();
	private Connection conn = DB.con;

	// 注册用户
	@Override
	public boolean addUser(User user) {
		// TODO Auto-generated method stub
		return false;
	}

	// 删除用户
	@Override
	public boolean deleteUser(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	// 更改用户
	@Override
	public boolean updateUser(User user, int id) {
		// TODO Auto-generated method stub
		return false;
	}

	// 查询用户
	@Override
	public List<?> selectUser() {
		return null;
	}
/*
	// 用户登录
	@Override
	public boolean loginUser(User user) {
		// TODO Auto-generated method stub
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			st = conn
					.prepareStatement("select * from user where USERNAME = ? and PASSWORD = ?");
			st.setString(1, user.getUserName());
			st.setString(2, user.getPassWord());
			rs = st.executeQuery();
			if (rs.next()) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DB.close(st, null);
		}
		return false;

	}*/

	// 登录用户校验
	@Override
	public User validateUser(String username, String password) {
		// TODO Auto-generated method stub
		List<User> list = new ArrayList<User>();
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			st = conn
					.prepareStatement("select * from user where USERNAME = ? and PASSWORD = ?");
			st.setString(1, username);
			st.setString(2, password);
			rs = st.executeQuery();
			while (rs.next()) {
				User user = new User();
				user.setUserName(rs.getString("username"));
				user.setPassWord(rs.getString("password"));
				list.add(user);
			}
			if (list.size() != 0) {
				return list.get(0);
			}else{
				return null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DB.close(st, null);
		}
		return null;
	}

}
